<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>小黑记事本</title>
  <script src="../js/vue.js"></script>
  <link rel="stylesheet" href="./css/index.css">

</head>



<body>
  <div id="app">
    <!-- 输入框 -->
    <header class="header">
      <h1>小黑记事本</h1>
      <input v-model.trim="iptList" @keyup.enter="add" placeholder="请输入任务" class="new-todo" />
      <button @click="add" class="add">添加任务</button>
    </header>
    <!-- 列表区域 -->
    <section class="main">
      <ul class="todo-list">
        <li v-for="(item,index) in list" :key="item.id" class="todo">
          <div class="view">
            <span class="index">{{index+1}}.</span> <label>{{item.task}}</label>
            <button @click="del(item.id)" class="destroy"></button>
          </div>
        </li>
      </ul>
    </section>
    <!-- 统计和清空 -->
    <footer v-if="list.length>0" class="footer">
      <!-- 统计 -->
      <span class="todo-count">合 计:<strong> {{list.length}} </strong></span>
      <!-- 清空 -->
      <button @click="clear" class="clear-completed">
        清空任务
      </button>
    </footer>
  </div>
</body>
</html>
<script>
  // 注意底部导航栏可以做一个v-if，如果没有数据就不显示了
  const app = new Vue({
    // 这个指的是上面的主体区域
    el: '#app',
    data: {
      iptList: '',
      list: [
        { id: 1, task: '跑步' },
        { id: 2, task: '游泳' }
      ]
    },
    methods: {
      del(id) {
        // 这一步是做筛选，筛选出满足条件的数组
        this.list = this.list.filter(item => item.id !== id)
      },
      add() {
        // 非空判断
        if (this.iptList.length === 0) {
          console.log(7877878);
          return alert("不允许输入为空哦")
        }
        // 增加数据
        this.list.unshift({
          id: +new Date(),
          task: this.iptList
        })
        // 清空输入框
        this.iptList = ''
      },
      clear() {
        this.list = []
      }
    },
  })
</script>